package com.lry.crawlers.listener;

import com.lry.crawlers.config.RabbitMQConfig;
import com.lry.crawlers.video.entity.VideoVariable;
import com.lry.crawlers.video.pipeline.VideoPipeline;
import com.lry.crawlers.video.processor.VideoProcessor;
import com.lry.crawlers.video.service.VideoService;
import com.lry.crawlers.video.service.VideoVariableService;
import com.lry.crawlers.information.entity.Information;
import com.lry.crawlers.information.service.InformationService;
import com.lry.crawlers.message.service.MessageService;
import com.lry.crawlers.util.ProcessVariable;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import java.util.Date;

@Component
public class videoMQListener {
    @Autowired
    VideoPipeline videoPipeline;
    @Autowired
    VideoVariableService videoVariableService;
    @Autowired
    VideoService videoService;
    @Autowired
    InformationService informationService;
    @Autowired
    MessageService messageService;



    //监听video_queue队列
    @RabbitListener(queues = RabbitMQConfig.VIDEOQUEUE_NAME)
    public void ListenerVideoQueue(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String name = new String(message.getBody());
            String[] split = name.split(",");
            Thread.sleep(3000);
            int i= informationService.updateInformation(new Information(split[1],"视频","采集关键词："+split[0],"进行中",new Date(),null));
            try {
            videoVariableService.update();
            //搜索内容
//            videoProcessor(split);

            for (VideoVariable v : ProcessVariable.vVariable.values()) {
                Spider.create(new VideoProcessor(v, videoService))
                        //设置爬取数据的地址
                        .addUrl(v.getSearchUrl()+split[0])
                        //setUUID只有在分布式的时候是必须的
                        .setUUID("12345678")
                        .addPipeline(videoPipeline)
                        //设置redisScheduler
                        //.setScheduler(new RedisScheduler(jedisPool))
                        //设置多线程
                        .thread(10)
                        //执行爬虫
                        .run();
            }
            channel.basicAck(deliveryTag, false);

            informationService.updateInformation(new Information(split[1],"视频","采集关键词："+split[0],"完成",null,new Date()));

        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, false);
            informationService.updateInformation(new Information(split[1],"视频","采集关键词："+split[0],"错误",null,new Date()));
            messageService.setMessage(new com.lry.crawlers.message.entity.Message("视频采集出现错误！title："+split[0]+"异常："+e,new Date()));
        }}

    }





